package com.huluobo.a2109monthdemo2.register

import android.widget.Toast
import androidx.lifecycle.ViewModelProvider
import com.blankj.utilcode.util.ToastUtils
import com.huluobo.a2109monthdemo2.R
import com.huluobo.a2109monthdemo2.base.BaseMVVMActivity
import com.huluobo.a2109monthdemo2.databinding.ActivityRegisterBinding

class RegisterActivity : BaseMVVMActivity<ActivityRegisterBinding>() {
    private lateinit var viewModel: RegisterViewModel
    override fun initData() {
        viewModel = ViewModelProvider(this)[RegisterViewModel::class.java]

        viewModel.registerSuccess.observe(this) {
            if (it.code == 200) {
                ToastUtils.showLong("注册成功:${it.data.username}")
                finish()
            } else {
                ToastUtils.showLong("注册失败:${it.message}")
            }
        }

        viewModel.registerFailed.observe(this) {
            ToastUtils.showLong("注册失败:$it")
        }
    }

    override fun initView() {
        dataBinding.registerToRegister.setOnClickListener {
            viewModel.register(
                dataBinding.registerUsername.text.toString(),
                dataBinding.registerPassword.text.toString(),
                dataBinding.registerRePassword.text.toString()
            )
        }
    }

    override fun getLayoutId(): Int = R.layout.activity_register
}